import { _decorator, Camera, Component, EventTouch, Input, input, instantiate, Label, Node, Prefab, Vec3 } from 'cc';
import { Global } from '../global';
const { ccclass, property } = _decorator;

@ccclass('initWorld')
export class initWorld extends Component {
    // 草地预制体1
    @property(Prefab)
    ground1: Prefab = null;
    // 草地预制体2
    @property(Prefab)
    ground2: Prefab = null;
    // 地图节点
    @property(Node)
    mapNode: Node = null;
    // player预制体
    @property(Prefab)
    player: Prefab = null;
    // stone预制体
    @property(Prefab)
    stone: Prefab = null;
    // iron预制体
    @property(Prefab)
    iron: Prefab = null;
    // food预制体
    @property(Prefab)
    food: Prefab = null;
    // hood预制体
    @property(Prefab)
    hood: Prefab = null;
    // sliver预制体
    @property(Prefab)
    sliver: Prefab = null;

    @property(Camera)
    private camera: Camera = null;
    private _moveSpeed: number = 1; // 移动速度

    // 地图中心点位置，用于发送请求
    private m = 0; 
    private n = 0;
    // 用于渲染模块
    private sx = 0;
    private sy = 0;
    // 换成本地坐标
    private lx = 0;
    private ly = 0;

    protected onLoad(): void {
        this.positionInit();
        this.mapMove();
        this.reqBlockData();
    }

    // 初始化摄像机的位置
    positionInit() {
        let x = parseInt(Global.UsrMg.x + '');
        let y = parseInt(Global.UsrMg.y + '');
        this.sx = 126 * (y - x);
        this.sy = 76 * (x + y);
    }
    
    // 移动地图
    mapMove() {
        input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
    }

    onTouchMove(event: EventTouch) {
        let lx = event.getDeltaX();
        let ly = event.getDeltaY();
        let x = this.camera.node.position.x; // 获取相机位置
        let y = this.camera.node.position.y;
        this.camera.node.setPosition(new Vec3(x - lx * this._moveSpeed, y - ly * this._moveSpeed, 0));
        let tx = parseInt(x - this.sx + '');
        let ty = parseInt(y - this.sy + '');
        let distance = parseInt(Math.sqrt(tx * tx + ty * ty) + '');
        if (distance > 600) {
            this.sx = x;
            this.sy = y;
            this.reqBlockData();
        }
    }

    // 获取周围的block数据
    reqBlockData() {
        this.getCenterPos();
        let m = this.m;
        let n = this.n;
        let data = {
            m,
            n
        }
        Global.http.sendRequest('/getBlockData', 'GET', data, (res) => {
            if (res.code == 1) {
                console.log("获取周围block数据失败", res.msg);
                return;
            } else {
                console.log("获取周围block数据成功", res.data);
                this.handleBlockData(res.data);
            }
        }, Global.cityServerUrl)
    }

    // 得到中心点位置转换成坐标
    getCenterPos() {
        let cx = parseInt(this.camera.node.position.x + '');
        let cy = parseInt(this.camera.node.position.y + '');
        let x = Math.round(cy / 152 - cx / 252); // 中心点x坐标
        let y = Math.round(cx / 252 + cx / 152);  // 中心点y坐标
        this.m = x;
        this.n = y;
    }

    // 处理请求返回数据
    handleBlockData(data) {
        let wallNode = this.node.getChildByName("wall");
        // 清除之前的孩子节点
        wallNode.removeAllChildren();
        for (let i = 0; i < data.length; i++) {
            let block = data[i];
            let x = block.x;
            let y = block.y;
            this.getLocalPos(x, y);
            // 生成草地
            this.createGrass(x, y);
            // 渲染
            switch (block.type) {
                case 'player':
                    let pNode = instantiate(this.player); // 创建玩家节点
                    let pLv = pNode.getChildByName('info').getChildByName('lvWorld').getChildByName('lvNum'); // 等级标签
                    let pName = pNode.getChildByName('info').getChildByName('name'); // 名字标签
                    wallNode.addChild(pNode);
                    pNode.setPosition(this.lx, this.ly + 20);
                    pLv.getComponent(Label).string = block.lv;
                    pName.getComponent(Label).string = block.name;
                    break;
                case 'food':
                    let fNode = instantiate(this.food);
                    let fLv = fNode.getChildByName('info').getChildByName('lvWorld').getChildByName('lvNum');
                    wallNode.addChild(fNode);
                    fNode.setPosition(this.lx, this.ly);
                    fLv.getComponent(Label).string = block.lv;
                    break;
                case 'hood':
                    let hNode = instantiate(this.hood);
                    let hLv = hNode.getChildByName('info').getChildByName('lvWorld').getChildByName('lvNum');
                    wallNode.addChild(hNode);
                    hNode.setPosition(this.lx, this.ly);
                    hLv.getComponent(Label).string = block.lv;
                    break;
                case 'stone':
                    let sNode = instantiate(this.stone);
                    let sLv = sNode.getChildByName('info').getChildByName('lvWorld').getChildByName('lvNum');
                    wallNode.addChild(sNode);
                    sNode.setPosition(this.lx, this.ly);
                    sLv.getComponent(Label).string = block.lv;
                    break;
                case 'sliver':
                    let slNode = instantiate(this.sliver);
                    let slLv = slNode.getChildByName('info').getChildByName('lvWorld').getChildByName('lvNum');
                    wallNode.addChild(slNode);
                    slNode.setPosition(this.lx, this.ly);
                    slLv.getComponent(Label).string = block.lv;
                    break;
                case 'iron':
                    let iNode = instantiate(this.iron);
                    let iLv = iNode.getChildByName('info').getChildByName('lvWorld').getChildByName('lvNum');
                    wallNode.addChild(iNode);
                    iNode.setPosition(this.lx, this.ly);
                    iLv.getComponent(Label).string = block.lv;
                    break;
                default:
                    console.log('未知');
                    break;
            }
        }
    }

    // 得到本地坐标
    getLocalPos(x, y) {
        let tx = parseInt(x);
        let ty = parseInt(y);
        this.lx = 126 * (ty - tx); // 计算x轴偏移量 m
        this.ly = 76 * (tx + ty); // 计算y轴偏移量 n
    }

    // 生成草地
    createGrass(x, y) {
        let sum = parseInt(x) + parseInt(y);
        let tNode = null;
        if (sum % 2 == 0) {
            tNode = instantiate(this.ground1);
        } else {
            tNode = instantiate(this.ground2);
        }
        this.mapNode.addChild(tNode);
        tNode.setPosition(this.lx, this.ly); // 设置位置
    }

    onDestroy() {
        input.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
    }
}


